<?php
/**
 * 【公告】接口控制器
 * 
 */
class noticeAction extends apiAction {
	
	/**
	 * 公告列表
	 */
	public function index(){
		$page	= I('page', 1);	// 要显示第几页数据
		$num	= I('num', 20);	// 每页显示多少条数据
		
		$data = array();
		
		$where= array('status'=>1);

		//限制当前用户只能看他相关城市的公告
		if($this->user['cityId']){
			$string = '';
			foreach ($this->user['cityId'] as $cityId){
				$string .= " FIND_IN_SET('{$cityId}',`regionId`) > 0 OR";
			}
			$where['_string'] = rtrim($string,'OR');
		}
		
		$notices = D('notice')->where($where)->order('ordid')->limit($num)->page($page)->select();
		if($notices){
			foreach ($notices as $row){
				$data[$row['id']]['id']		= $row['id'];
				$data[$row['id']]['title']	= $row['title'];
				$data[$row['id']]['date']	= $row['publishTime'] ? date('Y-m-d',$row['publishTime']) : date('Y-m-d',$row['editTime']);
			}
			$this->ajaxReturn(200, L('success'), $data);
		}else{
			$this->ajaxReturn(300,  L('no_more'));
		}
	}
	
	/**
	 * 公告详情
	 */
	public function detail(){
		$id		= I('id');
		$notice	= D('notice')->where(array('id'=>$id,'status'=>1))->find();
		
		if($notice){
			// 去除 style="white-space:nowrap;" 样式
			$notice['con'] = str_replace('style="white-space:nowrap;"','', $notice['con']);
			$notice['att'] = D(MODULE_NAME)->getAttachment($notice['att']);
			
			$this->assign('notice',$notice);
			$this->display();
		}else{
			$this->assign('msg', '公告未找到！');
			$this->display('public:alert');
		}
	}
}
/* End of file noticeAction.class.php */